<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:job="livcos.org/ns/process/job"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="c job xs f">

  <xsl:import href="../style/base.xsl" />
  
  <xsl:output encoding="UTF-8"
    doctype-public="-//W3C//DTD HTML 4.01//EN"
    doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
  
  <xsl:variable name="workObjId" select="'/localhost/system/work'"/>
  
  <xsl:template match="/">
    <html id="{$viewObjId}">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Jobs</title>
        <link href="../css/global.css" rel="stylesheet" type="text/css" />
        <link href="../css/webXml.css" rel="stylesheet" type="text/css" />
        <link href="../css/system_admin.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../script/util.js"><!-- --></script>
        <script type="text/javascript" src="../script/E.js"><!-- --></script>
        <script type="text/javascript" src="../script/cosmos.js"><!-- --></script>
      </head>
      <body id="{f:object-id(.)}">
        <xsl:variable name="engine" select="document(concat($workObjId,'.cosmos'))/*"/>
        <div class="head_line">
          <p>
            <img src="{$toDomainPath}/web/img/refresh.gif"
              onclick="document.location.reload(true)"/>
            <xsl:text> Cosmos Process Engine (max pending jobs: </xsl:text>
            <xsl:value-of select="$engine/@max-pending"/>
            <xsl:text>, max wait time: </xsl:text>
            <xsl:value-of select="$engine/@max-wait-time"/>
            <xsl:text>ms)</xsl:text>
          </p>
        </div>
        <div class="job_list">
          <xsl:apply-templates
            select="$engine/*[(position() &gt; (last()-51)) and (position() != last())]" mode="job">
            <xsl:sort select="name()" order="descending"/>
          </xsl:apply-templates>
        </div>
        <div class="content_panel" id="_content">
        </div>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="*" mode="job">
    <p>
      <a href="javascript:E('{name(.)}').toggle()">+</a>
      <xsl:text> </xsl:text>
      <a href="javascript:loadObject('{$toRootPath}', '{$workObjId}/{name(.)}', 'cosmos')">
        <xsl:if test="state[@type = 'FAILED']">
          <xsl:attribute name="style">color: red;</xsl:attribute>
        </xsl:if>
        <xsl:value-of select="name(.)" />
      </a>
      <xsl:if test="c:request">
        <xsl:text> (</xsl:text>
        <xsl:variable name="url" select="c:request/url/@path"/>
        <xsl:choose>
          <xsl:when test="string-length($url) > 32">
            <xsl:text>...</xsl:text>
            <xsl:value-of select="substring($url, string-length($url) - 31, 32)" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$url" />
          </xsl:otherwise>
        </xsl:choose>
        <xsl:text>)</xsl:text>
      </xsl:if>
      <xsl:text> </xsl:text>
      <xsl:value-of
        select="format-number(f:total-seconds(xs:dateTime(state[last()]/@time) - xs:dateTime(state[1]/@time)), '0')"/>
      <xsl:text>s </xsl:text>
      <xsl:value-of select="format-number(state[1]/@totalMemory div 1024 div 1024, '0')"/>
      <xsl:text>MB</xsl:text>
    </p>
    <div id="{name(.)}" style="display:none">
      <xsl:apply-templates select="*"/>
    </div>
  </xsl:template>

  <xsl:template name="indent">
    <xsl:for-each select="ancestor::*">
      <xsl:text>&#160;&#160;</xsl:text>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="*">
    <p>
      <xsl:call-template name="indent" />
      <xsl:variable name="xPath" select="concat('*[',count(preceding-sibling::*)+1,']')" />
      <a href="{$toRootPath}{$workObjId}/{name(..)}.cosmos?_xpath={$xPath}" target="sys_main">
        <xsl:value-of select="name(.)" />
      </a>
      <xsl:text> xmlns=</xsl:text>
      <xsl:value-of select="namespace-uri(.)" />
    </p>
  </xsl:template>

  <xsl:template match="state">
    <p>
      <xsl:call-template name="indent" />
      <xsl:value-of select="name(.)" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="@type" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="f:total-seconds(xs:dateTime(@time) - xs:dateTime(../state[1]/@time)) * 1000"/>
      <xsl:text>ms </xsl:text>
      <xsl:variable name="mem" select="@totalMemory - @freeMemory - ../state[1]/@totalMemory + ../state[1]/@freeMemory"/>
      <xsl:value-of select="format-number($mem div 1024 div 1024, '0.000')"/>
      <xsl:text>MB </xsl:text>
    </p>
  </xsl:template>
  
  <xsl:template match="state[1]">
    <p>
      <xsl:call-template name="indent" />
      <xsl:value-of select="name(.)" /> 
      <xsl:text> </xsl:text>
      <xsl:value-of select="@type" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="format-dateTime(xs:dateTime(@time), '[Y0001]-[M01]-[D01] [H01]:[m01]:[s01].[f001]')"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="format-number(@freeMemory div 1024 div 1024, '0.000')"/>
      <xsl:text>MB </xsl:text>
    </p>
  </xsl:template>
  
  <xsl:template match="c:request">
    <p>
      <xsl:call-template name="indent"/>
      <a href="{$toRootPath}{$workObjId}/{name(..)}/request.cosmos" target="sys_main">
        <xsl:value-of select="name(.)" />
      </a>
      <xsl:text> </xsl:text>
      <a href="{@ref}.cosmos" target="sys_main">
        <xsl:value-of select="@ref" />
      </a>
    </p>
  </xsl:template>

  <xsl:template match="c:response">
  </xsl:template>

  <xsl:template match="job:feed">
    <p>
      <xsl:call-template name="indent"/>
      <xsl:value-of select="name(.)" />
      <xsl:text> </xsl:text>
      <a href="{$toRootPath}{f:object-id(..)}/{name(..)}/{@stream}.view" target="sys_main">
        <xsl:value-of select="@stream" />
      </a>
      <xsl:text> in </xsl:text>
      <xsl:value-of select="@duration"/>
      <xsl:text>ms</xsl:text>
    </p>
    <xsl:apply-templates select="job:*"/>
  </xsl:template>

  <xsl:template match="job:resolve">
    <p>
      <xsl:call-template name="indent"/>
      <xsl:value-of select="name(.)" />
      <xsl:text> </xsl:text>
      <xsl:variable name="uri" select="if (@uri) then @uri else @ref"/>
      <a href="{$toRootPath}{$uri}" target="sys_main">
        <xsl:value-of select="$uri" />
      </a>
      <xsl:text> in </xsl:text>
      <xsl:value-of select="@duration"/>
      <xsl:text>ms</xsl:text>
    </p>
    <xsl:apply-templates select="job:*"/>
  </xsl:template>

  <xsl:template match="exception">
    <p>
      <xsl:call-template name="indent"/>
      <xsl:variable name="xPath" select="concat('*[',count(preceding-sibling::*)+1,']')"/>
      <a
        href="{$toRootPath}{$workObjId}/{name(..)}.cosmos?_xpath={$xPath}"
        target="sys_main">
        <xsl:value-of select="name(.)" />
      </a>
      <xsl:text> </xsl:text>
      <xsl:value-of select="@message" />
    </p>
  </xsl:template>

  <xsl:template match="job:update">
    <p>
      <xsl:call-template name="indent"/>
      <xsl:variable name="xPath" select="concat('*[',count(preceding-sibling::*)+1,']')"/>
      <a
        href="{$toRootPath}{$workObjId}/{name(..)}.cosmos?_xpath={$xPath}"
        target="sys_main">
        <xsl:value-of select="name(.)" />
      </a>
      <xsl:text> in </xsl:text>
      <xsl:value-of select="@duration"/>
      <xsl:text>ms</xsl:text>
    </p>
  </xsl:template>

  <xsl:template match="*" mode="metaXPath">
    <xsl:apply-templates select=".." mode="metaXPath"/>
    <xsl:text>*[</xsl:text>
    <xsl:value-of select="count(preceding-sibling::*)+1"/>
    <xsl:text>]/</xsl:text>
  </xsl:template>

  <xsl:template match="c:*" mode="metaXPath">
    <xsl:value-of select="local-name(.)"/>
    <xsl:text>.cosmos?_xpath=</xsl:text>
  </xsl:template>

  <xsl:template match="job:transform">
    <p>
      <xsl:call-template name="indent"/>
      <xsl:value-of select="name(.)" />
      <xsl:text> into </xsl:text>
      <xsl:variable name="metaXPath">
        <xsl:apply-templates select="result" mode="metaXPath"/>
        <xsl:text>node()</xsl:text>
      </xsl:variable>
      <a
        href="javascript:loadContent('{$toRootPath}{$workObjId}/{$metaXPath}')">
        <xsl:text>result</xsl:text>
      </a>
      <xsl:text> in </xsl:text>
      <xsl:value-of select="@duration"/>
      <xsl:text>ms</xsl:text>
    </p>
    <xsl:apply-templates select="job:*"/>
  </xsl:template>

</xsl:stylesheet>
